package plugin.template;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.Props;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.job.entries.sql.JobEntrySQL;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.di.ui.core.gui.WindowProperty;
import org.pentaho.di.ui.core.widget.StyledTextComp;
import org.pentaho.di.ui.core.widget.TextVar;
import org.pentaho.di.ui.trans.step.BaseStepDialog;


/**
 * 对话框类 
 * @Author      : http://www.ahuoo.com, ydh
 * @Date        : Jan 7, 2015
 * @Email       : ydh110100@163.com
 * @Version     : V1.0
 */
public class TemplateStepDialog extends BaseStepDialog implements StepDialogInterface {

	private static Class<?> PKG = TemplateStepMeta.class; // for i18n purposes
	
	private TemplateStepMeta input;

	private static final String[] FILETYPES = new String[] { BaseMessages.getString(PKG, "JobSQL.Filetype.Sql"), BaseMessages.getString(PKG, "JobSQL.Filetype.Text"), BaseMessages.getString(PKG, "JobSQL.Filetype.All") };

	private CTabFolder   wTabFolder;
	private FormData     fdTabFolder;

	private CTabItem     wMainTab, wFieldsTab;
	private FormData     fdMainComp, fdFieldsComp;	
	
    private Label wlName;

    private Text wName;

    private FormData fdlName, fdName;

    private Label wlUseSubs, wlAppend;

    private Button wUseSubs, wAppend;
    
    private Button wbVmFromFile;
    
    private Label wlVmFromFile;

    private FormData fdlUseSubs, fdUseSubs, fdlAppend, fdAppend;
    
    private FormData fdlVmFromFile, fdVmFromFile;

    private Label wlSQL;

    private StyledTextComp wSQL;

    private FormData fdlSQL, fdSQL;

    private Label wlPosition;

    private FormData fdlPosition;

    private Button wOK, wCancel;

    private Listener lsOK, lsCancel;

    private Shell shell;

    private SelectionAdapter lsDef;

    private boolean changed;
    

	// File
	private Label wlFilename, wlOutFilename;
	private Button wbFilename, wbOutFilename;
	private TextVar wFilename, wOutFilename;
	private FormData fdlFilename, fdbFilename
	, fdFilename, fdOutFilename
	, fdlOutFilename, fdbOutFilename;
	
	//vmofvm
	private Label wlVmofvmFromFile, wlVmofvmFromFile2, wlVmofvmPosition, wlVmofvm;
	private Button wbVmofvmFromFile, wbVmofvmFromFile2;
	private TextVar wVmofvmFile;
	private StyledTextComp wVmofvm;
	private FormData fdlVmofvmFromFile, fdlVmofvmFromFile2, fdlVmofvmPosition, fdlVmofvm,
	fdbVmofvmFromFile, fdbVmofvmFromFile2,
	fdVmofvmFile, fdVmofvm;
	
	
	public TemplateStepDialog(Shell parent, Object in, TransMeta transMeta, String sname) {
		super(parent, (BaseStepMeta) in, transMeta, sname);
		input = (TemplateStepMeta) in;
	}

	public String open()
    {
        Shell parent = getParent();
        Display display = parent.getDisplay();

		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN);
        props.setLook(shell);
        setShellImage(shell, input);

        ModifyListener lsMod = new ModifyListener()
        {
            public void modifyText(ModifyEvent e)
            {
            	input.setChanged();
            }
        };
        changed = input.hasChanged();

        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = Const.FORM_MARGIN;
        formLayout.marginHeight = Const.FORM_MARGIN;

        shell.setLayout(formLayout);
        shell.setText(BaseMessages.getString(PKG, "JobSQL.Title"));

        int middle = props.getMiddlePct();
        int margin = Const.MARGIN;

        wOK = new Button(shell, SWT.PUSH);
        wOK.setText(BaseMessages.getString(PKG, "System.Button.OK"));
        wCancel = new Button(shell, SWT.PUSH);
        wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel"));

        BaseStepDialog.positionBottomButtons(shell, new Button[] { wOK, wCancel }, margin, null);

        // Filename line
        wlName = new Label(shell, SWT.RIGHT);
        wlName.setText(BaseMessages.getString(PKG, "JobSQL.Name.Label"));
        props.setLook(wlName);
        fdlName = new FormData();
        fdlName.left = new FormAttachment(0, 0);
        fdlName.right = new FormAttachment(middle, 0);
        fdlName.top = new FormAttachment(0, margin);
        wlName.setLayoutData(fdlName);
        wName = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
        wName.setText(stepname);
        props.setLook(wName);
        wName.addModifyListener(lsMod);
        fdName = new FormData();
        fdName.left = new FormAttachment(middle, 0);
        fdName.top = new FormAttachment(0,  margin);
        fdName.right = new FormAttachment(100, 0);
        wName.setLayoutData(fdName);
        
        
        wTabFolder = new CTabFolder(shell, SWT.BORDER);
 		props.setLook(wTabFolder, Props.WIDGET_STYLE_TAB);
 		
 		fdTabFolder = new FormData();
		fdTabFolder.left   = new FormAttachment(0, 0);
		fdTabFolder.top    = new FormAttachment(wName, margin);
		fdTabFolder.right  = new FormAttachment(100, 0);		
		fdTabFolder.bottom = new FormAttachment(wOK, -margin);
		wTabFolder.setLayoutData(fdTabFolder);
		
        //////////////////////////
		// START OF KEY TAB    ///
		///
        wMainTab=new CTabItem(wTabFolder, SWT.NONE);
		wMainTab.setText("主模板"); //$NON-NLS-1$

		FormLayout mainLayout = new FormLayout ();
		mainLayout.marginWidth  = 3;
		mainLayout.marginHeight = 3;

		Composite wMainComp = new Composite(wTabFolder, SWT.NONE);
 		props.setLook(wMainComp);
		wMainComp.setLayout(mainLayout);
        
        // Vm from file?
        wlVmFromFile = new Label(wMainComp, SWT.RIGHT);
        wlVmFromFile.setText("从文件中获取模板");
        props.setLook(wlVmFromFile);
        fdlVmFromFile = new FormData();
        fdlVmFromFile.left = new FormAttachment(0, 0);
        fdlVmFromFile.top = new FormAttachment(wlName, 2*margin);
        fdlVmFromFile.right = new FormAttachment(middle, -margin);
        wlVmFromFile.setLayoutData(fdlVmFromFile);
        
        wbVmFromFile = new Button(wMainComp, SWT.CHECK);
        props.setLook(wbVmFromFile);
        wbVmFromFile.setToolTipText("从文件中获取模板");
        fdVmFromFile = new FormData();
        fdVmFromFile.left = new FormAttachment(middle, 0);
        fdVmFromFile.top = new FormAttachment(wlName, 2*margin);
        fdVmFromFile.right = new FormAttachment(100, 0);
        wbVmFromFile.setLayoutData(fdVmFromFile);
        wbVmFromFile.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
            	activeSQLFromFile();	
            	input.setChanged();
            }
        });


		// Filename line
		wlFilename = new Label(wMainComp, SWT.RIGHT);
		wlFilename.setText("模板文件名");
		props.setLook(wlFilename);
		fdlFilename = new FormData();
		fdlFilename.left = new FormAttachment(0, 0);
		fdlFilename.top = new FormAttachment(wlVmFromFile,  margin);
		fdlFilename.right = new FormAttachment(middle, -margin);
		wlFilename.setLayoutData(fdlFilename);

		wbFilename = new Button(wMainComp, SWT.PUSH | SWT.CENTER);
		props.setLook(wbFilename);
		wbFilename.setText(BaseMessages.getString(PKG, "System.Button.Browse"));
		fdbFilename = new FormData();
		fdbFilename.right = new FormAttachment(100, 0);
		fdbFilename.top = new FormAttachment(wbVmFromFile, margin);
		wbFilename.setLayoutData(fdbFilename);

		wFilename = new TextVar(transMeta, wMainComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		props.setLook(wFilename);
		wFilename.setToolTipText("请选择模板文件");
		wFilename.addModifyListener(lsMod);
		fdFilename = new FormData();
		fdFilename.left = new FormAttachment(middle, 0);
		fdFilename.top = new FormAttachment(wbVmFromFile,  margin);
		fdFilename.right = new FormAttachment(wbFilename, -margin);
		wFilename.setLayoutData(fdFilename);

		// Whenever something changes, set the tooltip to the expanded version:
		wFilename.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				wFilename.setToolTipText(transMeta.environmentSubstitute(wFilename.getText()));
			}
		});

		wbFilename.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				FileDialog dialog = new FileDialog(shell, SWT.OPEN);
				dialog.setFilterExtensions(new String[] { "*" });
				if (wFilename.getText() != null)
				{
					dialog.setFileName(transMeta.environmentSubstitute(wFilename.getText()));
				}
				dialog.setFilterNames(FILETYPES);
				if (dialog.open() != null)
				{
					wFilename.setText(dialog.getFilterPath() + Const.FILE_SEPARATOR
						+ dialog.getFileName());
				}
			}
		});
		
		// Filename line out
		wlOutFilename = new Label(wMainComp, SWT.RIGHT);
		wlOutFilename.setText("文件输出");
		props.setLook(wlOutFilename);
		fdlOutFilename = new FormData();
		fdlOutFilename.left = new FormAttachment(0, 0);
		fdlOutFilename.top = new FormAttachment(wFilename,  margin);
		fdlOutFilename.right = new FormAttachment(middle, -margin);
		wlOutFilename.setLayoutData(fdlOutFilename);

		wbOutFilename = new Button(wMainComp, SWT.PUSH | SWT.CENTER);
		props.setLook(wbOutFilename);
		wbOutFilename.setText(BaseMessages.getString(PKG, "System.Button.Browse"));
		fdbOutFilename = new FormData();
		fdbOutFilename.right = new FormAttachment(100, 0);
		fdbOutFilename.top = new FormAttachment(wFilename, margin);
		wbOutFilename.setLayoutData(fdbOutFilename);

		wOutFilename = new TextVar(transMeta, wMainComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		props.setLook(wOutFilename);
		wOutFilename.setToolTipText("请选择输出文件");
		wOutFilename.addModifyListener(lsMod);
		fdOutFilename = new FormData();
		fdOutFilename.left = new FormAttachment(middle, 0);
		fdOutFilename.top = new FormAttachment(wFilename,  margin);
		fdOutFilename.right = new FormAttachment(wbOutFilename, -margin);
		wOutFilename.setLayoutData(fdOutFilename);

		// Whenever something changes, set the tooltip to the expanded version:
		wOutFilename.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				wOutFilename.setToolTipText(transMeta.environmentSubstitute(wOutFilename.getText()));
			}
		});

		wbOutFilename.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				FileDialog dialog = new FileDialog(shell, SWT.OPEN);
				dialog.setFilterExtensions(new String[] { "*" });
				if (wOutFilename.getText() != null)
				{
					dialog.setFileName(transMeta.environmentSubstitute(wOutFilename.getText()));
				}
				dialog.setFilterNames(FILETYPES);
				if (dialog.open() != null)
				{
					wOutFilename.setText(dialog.getFilterPath() + Const.FILE_SEPARATOR
						+ dialog.getFileName());
				}
			}
		});
		
		// Append to file?
        wlAppend = new Label(wMainComp, SWT.RIGHT);
        wlAppend.setText("追加到文件后?");
        props.setLook(wlAppend);
        fdlAppend = new FormData();
        fdlAppend.left = new FormAttachment(0, 0);
        fdlAppend.top = new FormAttachment(wbOutFilename, margin);
        fdlAppend.right = new FormAttachment(middle, -margin);
        wlAppend.setLayoutData(fdlAppend);
        wAppend = new Button(wMainComp, SWT.CHECK);
        props.setLook(wAppend);
        wAppend.setToolTipText("追加到文件后");
        fdAppend = new FormData();
        fdAppend.left = new FormAttachment(middle, 0);
        fdAppend.top = new FormAttachment(wbOutFilename, margin);
        fdAppend.right = new FormAttachment(100, 0);
        wAppend.setLayoutData(fdAppend);
        wAppend.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                input.setAppend(!input.isAppend());
                input.setChanged();
            }
        });
		
        // Use variable substitution?
        wlUseSubs = new Label(wMainComp, SWT.RIGHT);
        wlUseSubs.setText(BaseMessages.getString(PKG, "JobSQL.UseVariableSubst.Label"));
        props.setLook(wlUseSubs);
        fdlUseSubs = new FormData();
        fdlUseSubs.left = new FormAttachment(0, 0);
        fdlUseSubs.top = new FormAttachment(wAppend, margin);
        fdlUseSubs.right = new FormAttachment(middle, -margin);
        wlUseSubs.setLayoutData(fdlUseSubs);
        wUseSubs = new Button(wMainComp, SWT.CHECK);
        props.setLook(wUseSubs);
        wUseSubs.setToolTipText(BaseMessages.getString(PKG, "JobSQL.UseVariableSubst.Tooltip"));
        fdUseSubs = new FormData();
        fdUseSubs.left = new FormAttachment(middle, 0);
        fdUseSubs.top = new FormAttachment(wAppend, margin);
        fdUseSubs.right = new FormAttachment(100, 0);
        wUseSubs.setLayoutData(fdUseSubs);
        wUseSubs.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                input.setUseVariableSubstitution(!input.isUseVariableSubstitution());
                input.setChanged();
            }
        });

        // show line number
        wlPosition = new Label(wMainComp, SWT.NONE);
        wlPosition.setText(BaseMessages.getString(PKG, "JobSQL.LineNr.Label", "0"));
        props.setLook(wlPosition);
        fdlPosition = new FormData();
        fdlPosition.left = new FormAttachment(0, 0);
        fdlPosition.right= new FormAttachment(100, 0);
        fdlPosition.bottom = new FormAttachment(100, -margin);
        wlPosition.setLayoutData(fdlPosition);

        // Script line
        wlSQL = new Label(wMainComp, SWT.NONE);
        wlSQL.setText("模板脚本：");
        props.setLook(wlSQL);
        fdlSQL = new FormData();
        fdlSQL.left = new FormAttachment(0, 0);
        fdlSQL.top = new FormAttachment(wUseSubs, margin);
        wlSQL.setLayoutData(fdlSQL);

        wSQL=new StyledTextComp(transMeta, wMainComp, SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, "");
        props.setLook(wSQL, Props.WIDGET_STYLE_FIXED);
        wSQL.addModifyListener(lsMod);
        fdSQL = new FormData();
        fdSQL.left = new FormAttachment(0, 0);
        fdSQL.top = new FormAttachment(wlSQL, margin);
        fdSQL.right = new FormAttachment(100, -10);
        fdSQL.bottom = new FormAttachment(wlPosition, -margin);
        wSQL.setLayoutData(fdSQL);
        
        fdMainComp = new FormData();
		fdMainComp.left  = new FormAttachment(0, 0);
		fdMainComp.top   = new FormAttachment(0, 0);
		fdMainComp.right = new FormAttachment(100, 0);
		fdMainComp.bottom= new FormAttachment(100, 0);
		wMainComp.setLayoutData(fdMainComp);

		wMainComp.layout();
		wMainTab.setControl(wMainComp);
		
		//
		// Fields tab...
		//
		wFieldsTab = new CTabItem(wTabFolder, SWT.NONE);
		wFieldsTab.setText("子模板"); //$NON-NLS-1$

		Composite wFieldsComp = new Composite(wTabFolder, SWT.NONE);
        props.setLook(wFieldsComp);
        
        FormLayout fieldsCompLayout = new FormLayout ();
        fieldsCompLayout.marginWidth  = Const.FORM_MARGIN;
        fieldsCompLayout.marginHeight = Const.FORM_MARGIN;
		wFieldsComp.setLayout(fieldsCompLayout);
		
		// xixi 分页内容从此开始
		// Vm from file?
        wlVmofvmFromFile = new Label(wFieldsComp, SWT.RIGHT);
        wlVmofvmFromFile.setText("从文件中获取模板的模板");
        props.setLook(wlVmofvmFromFile);
        fdlVmofvmFromFile = new FormData();
        fdlVmofvmFromFile.left = new FormAttachment(0, 0);
        fdlVmofvmFromFile.top = new FormAttachment(wlName, 2*margin);
        fdlVmofvmFromFile.right = new FormAttachment(middle, -margin);
        wlVmofvmFromFile.setLayoutData(fdlVmofvmFromFile);
        
        wbVmofvmFromFile = new Button(wFieldsComp, SWT.CHECK);
        props.setLook(wbVmofvmFromFile);
        wbVmofvmFromFile.setToolTipText("从文件中获取模板的模板");
        fdbVmofvmFromFile = new FormData();
        fdbVmofvmFromFile.left = new FormAttachment(middle, 0);
        fdbVmofvmFromFile.top = new FormAttachment(wlName, 2*margin);
        fdbVmofvmFromFile.right = new FormAttachment(100, 0);
        wbVmofvmFromFile.setLayoutData(fdbVmofvmFromFile);
        wbVmofvmFromFile.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
            	activeVmofvmFromFile();	
            	input.setChanged();
            }
        });


		// Filename line
        wlVmofvmFromFile2 = new Label(wFieldsComp, SWT.RIGHT);
        wlVmofvmFromFile2.setText("模板文件名");
		props.setLook(wlVmofvmFromFile2);
		fdbVmofvmFromFile2 = new FormData();
		fdbVmofvmFromFile2.left = new FormAttachment(0, 0);
		fdbVmofvmFromFile2.top = new FormAttachment(wlVmofvmFromFile,  margin);
		fdbVmofvmFromFile2.right = new FormAttachment(middle, -margin);
		wlVmofvmFromFile2.setLayoutData(fdbVmofvmFromFile2);

		wbVmofvmFromFile2 = new Button(wFieldsComp, SWT.PUSH | SWT.CENTER);
		props.setLook(wbVmofvmFromFile2);
		wbVmofvmFromFile2.setText(BaseMessages.getString(PKG, "System.Button.Browse"));
		fdbVmofvmFromFile2 = new FormData();
		fdbVmofvmFromFile2.right = new FormAttachment(100, 0);
		fdbVmofvmFromFile2.top = new FormAttachment(wbVmofvmFromFile, margin);
		wbVmofvmFromFile2.setLayoutData(fdbVmofvmFromFile2);

		wVmofvmFile = new TextVar(transMeta, wFieldsComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		props.setLook(wVmofvmFile);
		wVmofvmFile.setToolTipText("请选择模板文件");
		wVmofvmFile.addModifyListener(lsMod);
		fdVmofvmFile = new FormData();
		fdVmofvmFile.left = new FormAttachment(middle, 0);
		fdVmofvmFile.top = new FormAttachment(wbVmofvmFromFile,  margin);
		fdVmofvmFile.right = new FormAttachment(wbVmofvmFromFile2, -margin);
		wVmofvmFile.setLayoutData(fdVmofvmFile);

		// Whenever something changes, set the tooltip to the expanded version:
		wVmofvmFile.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				wVmofvmFile.setToolTipText(transMeta.environmentSubstitute(wVmofvmFile.getText()));
			}
		});

		wbVmofvmFromFile2.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				FileDialog dialog = new FileDialog(shell, SWT.OPEN);
				dialog.setFilterExtensions(new String[] { "*" });
				if (wVmofvmFile.getText() != null)
				{
					dialog.setFileName(transMeta.environmentSubstitute(wVmofvmFile.getText()));
				}
				dialog.setFilterNames(FILETYPES);
				if (dialog.open() != null)
				{
					wVmofvmFile.setText(dialog.getFilterPath() + Const.FILE_SEPARATOR
						+ dialog.getFileName());
				}
			}
		});
		
		// show line number
        wlVmofvmPosition = new Label(wFieldsComp, SWT.NONE);
        wlVmofvmPosition.setText(BaseMessages.getString(PKG, "JobSQL.LineNr.Label", "0"));
        props.setLook(wlVmofvmPosition);
        fdlVmofvmPosition = new FormData();
        fdlVmofvmPosition.left = new FormAttachment(0, 0);
        fdlVmofvmPosition.right= new FormAttachment(100, 0);
        fdlVmofvmPosition.bottom = new FormAttachment(100, -margin);
        wlVmofvmPosition.setLayoutData(fdlVmofvmPosition);

        // Script line
        wlVmofvm = new Label(wFieldsComp, SWT.NONE);
        wlVmofvm.setText("模板脚本(childvm)：");
        props.setLook(wlVmofvm);
        fdlVmofvm = new FormData();
        fdlVmofvm.left = new FormAttachment(0, 0);
        fdlVmofvm.top = new FormAttachment(wVmofvmFile, margin);
        wlVmofvm.setLayoutData(fdlVmofvm);

        wVmofvm = new StyledTextComp(transMeta, wFieldsComp, SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, "");
        props.setLook(wVmofvm, Props.WIDGET_STYLE_FIXED);
        wVmofvm.addModifyListener(lsMod);
        fdVmofvm = new FormData();
        fdVmofvm.left = new FormAttachment(0, 0);
        fdVmofvm.top = new FormAttachment(wlVmofvm, margin);
        fdVmofvm.right = new FormAttachment(100, -10);
        fdVmofvm.bottom = new FormAttachment(wlVmofvmPosition, -margin);
        wVmofvm.setLayoutData(fdVmofvm);
        
		// xixi 分页内容结束
		fdFieldsComp=new FormData();
		fdFieldsComp.left  = new FormAttachment(0, 0);
		fdFieldsComp.top   = new FormAttachment(0, 0);
		fdFieldsComp.right = new FormAttachment(100, 0);
		fdFieldsComp.bottom= new FormAttachment(100, 0);
		wFieldsComp.setLayoutData(fdFieldsComp);

		wFieldsComp.layout();
		wFieldsTab.setControl(wFieldsComp);
		
		wTabFolder.setSelection(0);
        // Add listeners
        lsCancel = new Listener()
        {
            public void handleEvent(Event e)
            {
                cancel();
            }
        };
        lsOK = new Listener()
        {
            public void handleEvent(Event e)
            {
                ok();
            }
        };

        wCancel.addListener(SWT.Selection, lsCancel);
        wOK.addListener(SWT.Selection, lsOK);

        lsDef = new SelectionAdapter()
        {
            public void widgetDefaultSelected(SelectionEvent e)
            {
                ok();
            }
        };

        wName.addSelectionListener(lsDef);

        // Detect X or ALT-F4 or something that kills this window...
        shell.addShellListener(new ShellAdapter()
        {
            public void shellClosed(ShellEvent e)
            {
                cancel();
            }
        });

        wSQL.addModifyListener(new ModifyListener()
        {
            public void modifyText(ModifyEvent arg0)
            {
                setPosition();
            }

	        }
	    );
		
		wSQL.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) { setPosition(); }
			public void keyReleased(KeyEvent e) { setPosition(); }
			} 
		);
		wSQL.addFocusListener(new FocusAdapter(){
			public void focusGained(FocusEvent e) { setPosition(); }
			public void focusLost(FocusEvent e) { setPosition(); }
			}
		);
		wSQL.addMouseListener(new MouseAdapter(){
			public void mouseDoubleClick(MouseEvent e) { setPosition(); }
			public void mouseDown(MouseEvent e) { setPosition(); }
			public void mouseUp(MouseEvent e) { setPosition(); }
			}
		);
		wSQL.addModifyListener(lsMod);
		
		// 
		 wVmofvm.addModifyListener(new ModifyListener()
	        {
	            public void modifyText(ModifyEvent arg0)
	            {
	            	setVmofvmPosition();
	            }

		        }
		    );
			
		 wVmofvm.addKeyListener(new KeyAdapter(){
				public void keyPressed(KeyEvent e) { setVmofvmPosition(); }
				public void keyReleased(KeyEvent e) { setVmofvmPosition(); }
				} 
			);
		 wVmofvm.addFocusListener(new FocusAdapter(){
				public void focusGained(FocusEvent e) { setVmofvmPosition(); }
				public void focusLost(FocusEvent e) { setVmofvmPosition(); }
				}
			);
		 wVmofvm.addMouseListener(new MouseAdapter(){
				public void mouseDoubleClick(MouseEvent e) { setVmofvmPosition(); }
				public void mouseDown(MouseEvent e) { setVmofvmPosition(); }
				public void mouseUp(MouseEvent e) { setVmofvmPosition(); }
				}
			);
		 wVmofvm.addModifyListener(lsMod);

        getData();
        activeSQLFromFile();
        activeVmofvmFromFile();

        BaseStepDialog.setSize(shell);

        shell.open();
        props.setDialogSize(shell, "JobSQLDialogSize");
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        return stepname;
    }

	public void setPosition(){
		
		String scr = wSQL.getText();
		int linenr = wSQL.getLineAtOffset(wSQL.getCaretOffset())+1;
		int posnr  = wSQL.getCaretOffset();
				
		// Go back from position to last CR: how many positions?
		int colnr=0;
		while (posnr>0 && scr.charAt(posnr-1)!='\n' && scr.charAt(posnr-1)!='\r')
		{
			posnr--;
			colnr++;
		}
		wlPosition.setText(BaseMessages.getString(PKG, "JobSQL.Position.Label",""+linenr,""+colnr));

	}
	
	public void setVmofvmPosition(){
		
		String scr = wVmofvm.getText();
		int linenr = wVmofvm.getLineAtOffset(wVmofvm.getCaretOffset())+1;
		int posnr  = wVmofvm.getCaretOffset();
				
		// Go back from position to last CR: how many positions?
		int colnr=0;
		while (posnr>0 && scr.charAt(posnr-1)!='\n' && scr.charAt(posnr-1)!='\r')
		{
			posnr--;
			colnr++;
		}
		wlVmofvmPosition.setText(BaseMessages.getString(PKG, "JobSQL.Position.Label",""+linenr,""+colnr));

	}
	
    public void dispose()
    {
        WindowProperty winprop = new WindowProperty(shell);
        props.setScreen(winprop);
        shell.dispose();
    }

    /**
     * Copy information from the meta-data input to the dialog fields.
     */
    public void getData()
    {
        if (stepname != null)
            wName.setText(stepname);
        if (input.getSql() != null)
            wSQL.setText(input.getSql());

        wUseSubs.setSelection(input.isUseVariableSubstitution());
        wbVmFromFile.setSelection(input.isSqlfromfile());
        
        if (input.getSqlfilename() != null)
        	wFilename.setText(input.getSqlfilename());
        
        if (input.getSql() != null)
            wSQL.setText(input.getSql());
        
        if (input.getOutfilename() != null)
        	wOutFilename.setText(input.getOutfilename());
        
        wAppend.setSelection(input.isAppend());
        wbVmofvmFromFile.setSelection(input.isVmofvmfromfile());
        if (input.getVmofvmfile() != null)
            wVmofvmFile.setText(input.getVmofvmfile());
        if (input.getVmofvm() != null)
            wVmofvm.setText(input.getVmofvm());
        
        wName.selectAll();
    }
    
    private void activeSQLFromFile()
    {
    	wlFilename.setEnabled(wbVmFromFile.getSelection());
    	wFilename.setEnabled(wbVmFromFile.getSelection());
    	wbFilename.setEnabled(wbVmFromFile.getSelection());
    	wSQL.setEnabled(!wbVmFromFile.getSelection());
    	wlSQL.setEnabled(!wbVmFromFile.getSelection());
    	wlPosition.setEnabled(!wbVmFromFile.getSelection());
    }
    
    private void activeVmofvmFromFile(){
    	wlVmofvmFromFile2.setEnabled(wbVmofvmFromFile.getSelection());
    	wVmofvmFile.setEnabled(wbVmofvmFromFile.getSelection());
    	wbVmofvmFromFile2.setEnabled(wbVmofvmFromFile.getSelection());
    	wVmofvm.setEnabled(!wbVmofvmFromFile.getSelection());
    	wlVmofvm.setEnabled(!wbVmofvmFromFile.getSelection());
    	wlVmofvmPosition.setEnabled(!wbVmofvmFromFile.getSelection());
    }

    private void cancel()
    {
        input.setChanged(changed);
        input = null;
        dispose();
    }

    private void ok()
    {
 	   if(Const.isEmpty(wName.getText())) 
      {
			MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR );
			mb.setText(BaseMessages.getString(PKG, "System.StepJobEntryNameMissing.Title"));
			mb.setMessage(BaseMessages.getString(PKG, "System.JobEntryNameMissing.Msg"));
			mb.open(); 
			return;
      }
 	   	stepname = wName.getText();
        input.setSql(wSQL.getText());
        input.setUseVariableSubstitution(wUseSubs.getSelection());
        input.setSqlfromfile(wbVmFromFile.getSelection());
        input.setSqlfilename(wFilename.getText());
        input.setOutfilename(wOutFilename.getText());
        input.setAppend(wAppend.getSelection());
        
        input.setVmofvmfromfile(wbVmofvmFromFile.getSelection());
        input.setVmofvmfile(wVmofvmFile.getText());
        input.setVmofvm(wVmofvm.getText());
        
        dispose();
    }
}
